/******************************************************************************
* take variable selection outputs from R to project place effects on correlates
*****************************************************************************/


*********************************************************************
//plot projection after R (Figure A15-A20)
*********************************************************************

global ver 2


* load R lasso result
import delimited using "${outDataDir}/post_lasso_v${ver}.csv", ///
	delimiters(",") varnames(1) clear
rename v1 _j

save "$dataRoot/post_lass_v${ver}", replace

use "$dataRoot/TU_place_vs_customer_cov_std.dta", clear

if $ver==2{
	global proj_covar_adj bkrty3_p ${proj_covar}
	global morecov bkrty3_p
	global proj_mv_vars bkrt7y3 bkrt13y3
}
else{
	global proj_covar_adj ${proj_covar}
	global morecov 
	global proj_mv_vars colmed bkrt7y3 bkrt13y3 bkrty3
}

*** plot correlates ***
local xn = 0
foreach cov of varlist $proj_covar{
	local xn = `xn'+1

	
	local yn = 0
	local reshapeStr = ""
	foreach var of varlist $proj_mv_vars{	
		local yn = `yn'+1


		*correlation with covariates
		qui reg `var'_fe `cov' ${morecov} [aw=numobs], robust

		figShowSlope "`var'"  "fe_`xn'"
		gen `var'_fe_b_`xn'  = ${fe_`xn'_`var'_1}
		gen `var'_fe_se_`xn' = ${fe_`xn'_`var'_2}
		
		qui reg `var'_p  `cov' ${morecov} [aw=numobs], robust

		figShowSlope "`var'"  "p_`xn'"
		gen `var'_p_b_`xn'  = ${p_`xn'_`var'_1}
		gen `var'_p_se_`xn' = ${p_`xn'_`var'_2}
		
		
		local reshapeStr = "`reshapeStr' `var'_fe_b_ `var'_p_b_  `var'_fe_se_ `var'_p_se_"
	}
}

gen year=2015
collapse $proj_mv_vars *_fe_* *_p_* [aw=numobs], by(year)
reshape long "`reshapeStr'", i(year)


*join with r post lasso results
gen id = -_j 
replace _j = _j+${ver}-1 //offset one index to drop bkrty3_p in version 2
merge 1:1 _j using "$dataRoot/post_lass_v${ver}"
keep if _merge==3 //note that this will drop bkrty3_p in version 2
drop _merge


*remove zero for not selected variables in post-lasso
foreach pl of varlist *_pl{
	replace `pl' = . if `pl'==0
}



**** programmatically set up labels
	
*break covaraites into categories
*break in labels
local b1=-5
local b2=-10

*tital of each category
local btxt0 = "Legal Factors"
local btxt1 = "Credit Supply"
local btxt2 = "Local Econ Factors"

local catn= 2

*names of covariates

*legal factors
local i=1
local cov`i' = "Median seizable asset"
local i=`i'+1	
local cov`i' = "Wage garnishment"
local i=`i'+1	
local cov`i' = "Chapter 7 fees"
local i=`i'+1	
local cov`i' = "Chapter 13 fees"
local i=`i'+1	

*credit supply
local cov`i' = "Credit limit"
local i=`i'+1	
local cov`i' = "Branch/sq mile"
local i=`i'+1	
local cov`i' = "National banks"
local i=`i'+1	

*local econ factors
local cov`i' = "Median income" 
local i=`i'+1
local cov`i' = "Income inequality" 
local i=`i'+1	
local cov`i' = "Own house" 			
local i=`i'+1	
local cov`i' = "Median house value" 	
local i=`i'+1	
local cov`i' = "Own vehicle" 		
local i=`i'+1	
local cov`i' = "Bachelor degree" 	 					
local i=`i'+1	
local cov`i' = "Employment" 
local i=`i'+1	
local cov`i' = "Health insurance"
local i=`i'+1	
local cov`i' = "For-profit hospital"

*a place holder at the end
local i=`i'+1	
local cov`i' = " "

*label each cov
local ttlbl = `i' + 2*`catn'
di "`ttlbl' labels in total"

local covlbl1 "ylabel( "
local covlbl2 "ylabel( "
local j=0
forval i=1/`ttlbl'{
	if !inlist(-`i'+1,`b1',`b2') &  !inlist(-`i',`b1',`b2'){
		local j=`j'+1
		local covlbl1 `" `covlbl1'  -`i' "`cov`j''"  "'
		local covlbl2 `" `covlbl2'  -`i' " "  "'
	}
}


*format category titles
local sectlbl1 `"ylabel( 1 " " 0 "{bf:`btxt0'}""'
local sectlbl2 `"ylabel( 1 " " 0 " " "'

*offset id so we have break between categories of covaraites
forval i=1/`catn'{
	
	local bline=`b`i''-1
	
	replace id=id-2 if id<=`b`i''	
	local sectlbl1 `" `sectlbl1' `bline'  "{bf:`btxt`i''}" "'
	local sectlbl2 `" `sectlbl2' `bline'  " " "'
}

*finishing of ylables
forval i=1/2{
	local covlbl`i'  `" `covlbl`i''  , angle(0) labsize(small)  labgap(1) notick grid)  "'
	local sectlbl`i' `" `sectlbl`i'' , angle(0) labsize(small)  labgap(2)  custom add)  "'
	
}



di `"`sectlbl1'"'
di `"`sectlbl2'"'
di `"`covlbl1'"'
di `"`covlbl2'"'	




**** generate plots for each mover variable
foreach var of varlist  $proj_mv_vars{	

	*state means
	gen `var'_1   = `var'_fe_b_
	gen `var'_1_l = `var'_1 - `var'_fe_se_*1.96
	gen `var'_1_h = `var'_1 + `var'_fe_se_*1.96

	gen `var'_1_pl   = `var'_fe_b_pl
	gen `var'_1_pl_l = `var'_1_pl - `var'_fe_se_pl*1.96
	gen `var'_1_pl_h = `var'_1_pl + `var'_fe_se_pl*1.96
	
	
	*place effect
	gen `var'_2   = `var'_p_b_
	gen `var'_2_l = `var'_2 - `var'_p_se_*1.96
	gen `var'_2_h = `var'_2 + `var'_p_se_*1.96

	gen `var'_2_pl   = `var'_p_b_pl
	gen `var'_2_pl_l = `var'_2_pl - `var'_p_se_pl*1.96
	gen `var'_2_pl_h = `var'_2_pl + `var'_p_se_pl*1.96	

	
	forval i=1/2{
		*state variation
		twoway ///
			(rcap `var'_`i'_l `var'_`i'_h id, horizontal) ///
			(scatter  id `var'_`i',  msize(vsmall) ) ///
			, xline(0, lcolor(black) lwidth(thin)) legend(off) graphregion(fcolor(white))  ///
			plotregion(ilstyle(solid) ilcolor(black) lwidth(vthin) margin(zero)) ///
			title("Bivariate OLS", size(medsmall) color(black) box bexpand fcolor(gs14) lcolor(black) lwidth(medthin)) ///
			xlabel(-1(.5)1, labsize(small) glwidth(vthin) noticks) xscale(range(-1.05(.1)1.05)) ylabel(, glwidth(vthin)) ///
			ytitle("") yscale(range(-20(1)1)) name(p1, replace) ///
			`covlbl1' `sectlbl1'
		
		*place effect
		twoway ///
			(rcap `var'_`i'_pl_l `var'_`i'_pl_h id, horizontal) ///
			(scatter  id `var'_`i'_pl,  msize(small) msymbol(Oh)) ///
			, xline(0, lcolor(black) lwidth(thin)) legend(off) graphregion(fcolor(white))  ///
			plotregion(ilstyle(solid) ilcolor(black) lwidth(vthin) margin(zero)) ///
			title("Post-Lasso", size(medsmall) color(black) box bexpand fcolor(gs14) lcolor(black) lwidth(medthin)) ///
			xlabel(-1(.5)1, labsize(small) glwidth(vthin) noticks) xscale(range(-1.05(.1)1.05)) ylabel(, glwidth(vthin)) ///
			ytitle("") yscale(range(-`ttlbl'(1)1)) name(p2, replace) ///
			`covlbl2' `sectlbl2' fxsize(58)
	
		if `i'==1{
			local ext "fe"
		}
		else{
			local ext "p"
		}
		graph combine p1 p2, rows(1) graphregion(fcolor(white)) imargin(0 0 0 0) 
		
		if $ver==1{
			graph export "${figGeoDebt}/project_place_person_`ext'_`var'.png", replace
		}
		else{
			graph export "${figGeoDebt}/project_place_person_`ext'_bk_`var'.png", replace
		}
	}	
	
	
	
}



/*
*close all graph windows
window manage close graph _all
